﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTArticle : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private DataTable dtProductCategorys = new DataTable();
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 DanhMucID = -1;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 30;
        private Int32 totalRecord;
        private String Search = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(EHTWebsiteProject.Administrator.EHTArticle));
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {
                    //lấy tất cả danh sách chuyên mục tin tức
                    dtProductCategorys = DanhMucTinTucProvider.GetDanhMucTinTuc().Tables[0];

                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }

                    try
                    {
                        DanhMucID = Int32.Parse(Request.QueryString["DanhMucID"].ToString());
                    }
                    catch
                    {
                        DanhMucID = -1;
                    }

                    try
                    {
                        Search = Request.QueryString["Search"].ToString();
                    }
                    catch
                    {
                        Search = "";
                    }

                    try
                    {
                        pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
                    }
                    catch
                    {
                        pageIndex = 1;
                    }

                    try
                    {
                        pageSize = Int32.Parse(Request.QueryString["PageSize"].ToString());
                        Session["PageSize"] = pageSize.ToString();
                    }
                    catch
                    {
                        pageSize = 30;
                    }

                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadLanguage(Thread.CurrentThread.CurrentCulture);
                    LoadTinTuc(DanhMucID);

                    ddlSize.SelectedValue = pageSize.ToString();

                    CreateDDLCategory(0, 0);
                    if (IsVN == true)
                        this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                    else
                        this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));
                    ddlDanhmucSanPham.SelectedValue = DanhMucID.ToString();
                }
            }
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbAddNew.Text = rm.GetString("lbAddNew", ci);
            lbDel.Text = rm.GetString("lbDel", ci);
            lbEdit.Text = rm.GetString("lbEdit", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbArticle.Text = rm.GetString("lbArticle", ci);
            lbHienThi.Text = rm.GetString("lbHienThi", ci);
            lbTat.Text = rm.GetString("lbTat", ci);
            lbBat.Text = rm.GetString("lbBat", ci);

        }
        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucTinTucID"].ToString());
                ListItem li;
                if (IsVN == true)
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucVN"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                else
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucOther"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                this.ddlDanhmucSanPham.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }

        /// <summary>
        /// Hàm lấy tất cả các tin
        /// </summary>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public TinTuc GetTinTuc()
        {
            TinTuc theTinTuc = new TinTuc();
            theTinTuc.DanhMucTinTucID = DanhMucID;
            //lấy theo ngôn ngữ mặc định
            if (IsVN == true)
            {
                theTinTuc.TieuDeVN = Search;
                theTinTuc.TieuDeOther = "";
            }
            //lấy theo ngôn ngữ khác
            else
            {
                theTinTuc.TieuDeOther = Search;
                theTinTuc.TieuDeVN = "";
            }

            return theTinTuc;
        }
        /// <summary>
        /// Hàm lấy danh mục tin theo ID
        /// </summary>
        /// <param name="DanhMucID"></param>

        [AjaxPro.AjaxMethod]
        public void LoadTinTuc(Int32 DanhMucID)
        {
            //lấy theo ngôn ngữ mặc định
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                rpNews.DataSource = TinTucProvider.GetTinTucsByFillter(GetTinTuc(), pageIndex, pageSize, out totalRecord);
                rpNews.DataBind();
                pnTinTucOther.Visible = false;
            }
            //lấy theo ngôn ngữ khác
            else
            {
                rpNewsOther.DataSource = TinTucProvider.GetTinTucsByFillter(GetTinTuc(), pageIndex, pageSize, out totalRecord);
                rpNewsOther.DataBind();
                pnTinTuc.Visible = false;
            }
            //phân trang 
            this.PaggingControl1.PageIndex = pageIndex;
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PageURL = "EHTArticle.aspx?DanhMucID=" + DanhMucID.ToString();
        }
        /// <summary>
        /// Ham check all hoặc uncheck all
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CheckAll(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                //check repeter theo ngôn ngữ mặc định
                if (ckAll.Checked)
                {
                    //duyệt mảng
                    foreach (RepeaterItem rp in rpNews.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpNews.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
            else
            {
                //check theo ngôn ngữ khác
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpNewsOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpNewsOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
        }

        protected void rpNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            Image ImagStatus = (Image)e.Item.FindControl("ImagStatus");
            try
            {
                if (lbStatus.Text == "True")
                    ImagStatus.ImageUrl = "~/images/tick.png";
                else
                    ImagStatus.ImageUrl = "~/images/Close.png";
            }
            catch
            {

            }
        }
        /// <summary>
        /// Hàm unpublish
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImaTat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpNews.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpNewsOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }

            for (int i = 0; i < arr.Count; i++)
                TinTucProvider.ChangePublish(Int32.Parse(arr[i].ToString()), false);
            LoadTinTuc(DanhMucID);
        }

        /// <summary>
        ///Hàm publish 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImgBat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpNews.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpNewsOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            for (int i = 0; i < arr.Count; i++)
                TinTucProvider.ChangePublish(Int32.Parse(arr[i].ToString()), true);
            LoadTinTuc(DanhMucID);
        }

        /// <summary>
        /// Hàm xóa
        /// Duyệt tất cả các record cần xóa gán vào một mảng rồi đọc mảng để xóa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpNews.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpNewsOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }

            for (int i = 0; i < arr.Count; i++)
                TinTucProvider.DeleteTinTucByTinTucID(Int32.Parse(arr[i].ToString()));
            LoadTinTuc(DanhMucID);
        }

        /// <summary>
        /// Hàm check edit record
        /// chỉ được chọn 1 record để sửa nhiều hơn thì báo lỗi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            Int32 ID = 0;
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpNews.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpNewsOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }
            if (Count > 1 || Count == 0)
            {
                String messa = "";
                if (IsVN == true)
                    messa = HangSo.Edit_VN;
                else
                    messa = HangSo.Edit_Other;
                ltrErorr.Text = HelpMethod.ErorrLogin(messa);
            }
            else
            {
                Response.Redirect("EHTArticleIU.aspx?TinTucID=" + ID.ToString());
            }
        }

        protected void ddlDanhmucSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("EHTArticle.aspx?DanhMucID=" + ddlDanhmucSanPham.SelectedValue);
        }

        protected void ImagSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("EHTArticle.aspx?DanhMucID=" + DanhMucID + "&&Search=" + txtsearch.Text);
        }

        protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("EHTArticle.aspx?PageSize=" + ddlSize.SelectedValue);
        }

        protected void rpNewsOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            Image ImagStatus = (Image)e.Item.FindControl("ImagStatus");
            try
            {
                if (lbStatus.Text == "True")
                    ImagStatus.ImageUrl = "~/images/tick.png";
                else
                    ImagStatus.ImageUrl = "~/images/Close.png";
            }
            catch
            {

            }
        }

        protected void imgConfig_Click(object sender, ImageClickEventArgs e)
        {
            Session.Remove("ArrayListConfig");
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpNews.Items)
                {
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    String str = lbDelID.Text;
                    arr.Add(str);

                }
            }
            else
            {
                foreach (RepeaterItem rp in rpNewsOther.Items)
                {
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    String str = lbDelID.Text;
                    arr.Add(str);

                }
            }
            if (arr.Count > 0)
            {
                Session["ArrayListConfig"] = arr;
                Response.Redirect("EHTConfigNews.aspx");
            }
            else
                return;
        }
    }
}